package com.sangananitsolution.soapApi;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;

public class LoginValidate extends AsyncTask<String, Void, String> {
	String isvaliduser;
	Context context;
	String email, password;
	//private ProgressDialog pDialog;
	private final String URL = "http://garagesalesagogo.com.au/AuthenticationService.asmx?op=ValidateLogin";
	private final String SOAP_ACTION = "http://tempuri.org/ValidateLogin";
	private String TAG = "PGGURU";

	public LoginValidate(Context context) {
		// All variable declaration is goes here
		this.context = context;
		SharedPreferences sharedPreferencesLoginClass = context
				.getSharedPreferences("userInfoMainActvity",
						Context.MODE_PRIVATE);
		email = sharedPreferencesLoginClass.getString("Email", "vp");
		password = sharedPreferencesLoginClass.getString("Password", "vp");

	}

	@Override
	protected String doInBackground(String... params) {
		// background code goes here

		SoapObject validateLoginobj = null;
		// Create request

		validateLoginobj = new SoapObject("http://tempuri.org/",
				"ValidateLogin");
		validateLoginobj.addProperty("userName", email);
		validateLoginobj.addProperty("password", password);

		// Create envelope
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;

		// Set output SOAP object
		envelope.setOutputSoapObject(validateLoginobj);

		envelope.implicitTypes = true;

		// Create HTTP call object
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		SoapObject validateLoginResponseobj = null;
		SoapObject validateLoginResultobj = null;
		SoapObject locationobj = null;
		SoapObject loginStatusobj = null;

		try {
			// Invole web service
			androidHttpTransport.call(SOAP_ACTION, envelope);
			// Get the response
			validateLoginResponseobj = (SoapObject) envelope.bodyIn;
			validateLoginResultobj = (SoapObject) validateLoginResponseobj
					.getProperty(0);
			String useremail = validateLoginResultobj
					.getPropertyAsString("EmailAddress");
			String fname = validateLoginResultobj
					.getPropertyAsString("FirstName");

			String lname = validateLoginResultobj
					.getPropertyAsString("LastName");
			String phone = validateLoginResultobj
					.getPropertyAsString("PhoneNumber");

			String country = validateLoginResultobj
					.getPropertyAsString("Country");

			locationobj = (SoapObject) validateLoginResultobj
					.getProperty("Location");
			String longitute = locationobj.getPropertyAsString("Longitude");
			String latitute = locationobj.getPropertyAsString("Latitude");
/*
			String imageinfo = validateLoginResultobj
					.getPropertyAsString("UserImage");

			String facebookId = validateLoginResultobj
					.getPropertyAsString("FacebookId");*/
			loginStatusobj = (SoapObject) validateLoginResultobj
					.getProperty("LoginStatus");
			isvaliduser = loginStatusobj
					.getPropertyAsString("IsValidUser");
			String statusmsg = loginStatusobj
					.getPropertyAsString("StatusMessage");
			String userid = validateLoginResultobj
					.getPropertyAsString("UserId");

			Log.i(TAG, useremail);

			Log.i(TAG, fname);
			Log.i(TAG, lname);
			Log.i(TAG, phone);
			Log.i(TAG, country);
			Log.i(TAG, longitute);
			Log.i(TAG, latitute);
			// Log.i(TAG, facebookId);
			Log.i(TAG, isvaliduser);
			Log.i(TAG, statusmsg);
			Log.i(TAG, userid);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	@Override
	protected void onPostExecute(String result) {
		
		super.onPostExecute(result);
		
		
		//new MainActivity().checkStatus(isvaliduser);

	}

	@Override
	protected void onPreExecute() {
	
		super.onPreExecute();
	}

	@Override
	protected void onProgressUpdate(Void... values) {
	
		super.onProgressUpdate(values);
	}

}
